﻿using CommandProcessor.Dispatcher;
using Data.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using AutoMapper;
using QuanLySanXuat.Models;
using Domain.Commands;
using Core.Common;
using Core.Extensions;
using Data.Models;
namespace QuanLySanXuat.Controllers
{
    public class ProductTypeController : Controller
    {
        private readonly ICommandBus commandBus;
        private readonly IProductTypeRepository productTypeRepository;
        public ProductTypeController(ICommandBus commandBus, IProductTypeRepository productTypeRepository)
        {
            ModelState.Clear(); 
            this.commandBus = commandBus;
            this.productTypeRepository = productTypeRepository;
        }
        //
        // GET: /ProductType/
         [HttpGet]
        public ActionResult Index()
        {
            var prodcutType = productTypeRepository.GetAll();
            return View(prodcutType.ToPagedList(1, 10));
        }
        [HttpGet]
         public ActionResult Create()
         {
             ModelState.Clear();
             return View();
         }
        [HttpPost]
        public ActionResult Save(int ID, string Name, string Description, string Remark, int Status)
        {
            var form = new ProductTypeFormModels()
            {
                ID=ID,
                Name = Name,
                Description = Description,
                Remark = Remark,
                Status = Status,
            };
                var command = Mapper.Map<ProductTypeFormModels, CreateOrUpdateProductTypeCommand>(form);
                IEnumerable<ValidationResult> errors = commandBus.Validate(command);
                ModelState.AddModelErrors(errors);
                var result = commandBus.Submit(command);
                  
               
                var model = productTypeRepository.GetAll();
                return PartialView("_ProductTypeList", model.ToPagedList(1, 10));
       }
        [HttpGet]
        public JsonResult Get(int ID)
        {
            ProductType product = productTypeRepository.GetById(ID);
            var obj = Mapper.Map<ProductType, ProductTypeFormModels>(product);
            return Json(obj, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult RemoveItems(string[] ProID)
        {
            List<DeleteProductTypeCommand> list = new List<DeleteProductTypeCommand>();
            foreach (var item in ProID)
            {
                var command = new DeleteProductTypeCommand { ID = Convert.ToInt16(item) };
                list.Add(command);
            }
            var result = commandBus.SubmitRange(list);
            var model = productTypeRepository.GetAll();
            return PartialView("_ProductTypeList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        public ActionResult Search(string Name)
        {
            IEnumerable<ProductType> model;
            if (String.IsNullOrEmpty(Name))
            {
                model = productTypeRepository.GetAll();
            }
            else
            {
                model = productTypeRepository.GetMany(x => (x.Name.Contains(Name)) || (x.Description.Contains(Name)) || (x.Remark.Contains(Name)));
            
            }
            return PartialView("_ProductTypeList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        [ValidateInput(false)]
        public ActionResult Page(int PageID)
        {
            var prodcutType = productTypeRepository.GetAll();
            return PartialView("_ProductTypeList", prodcutType.ToPagedList(1, PageID));
        }
    }
}
